package ar.com.spsolutions.splibrary.repositories;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import ar.com.spsolutions.splibrary.author.Author;

public class AuthorRepository extends HibernateGenericDAO<Author> implements
		GenericRepository<Author> {

	private static final long serialVersionUID = -6490590159507547228L;
	private static final String NAME_PROPERTY = "name";
	private static final String UNCHECKED = "unchecked";

	public AuthorRepository() {
		super(Author.class);
	}

	public Author findByName(final String name) {
		return this.findByEq("name", name);
	}

	public int sizeOfSearch(final String searchValue) {
		final DetachedCriteria criteria = this.createDetachableCriteria();
		criteria.add(Restrictions.ilike(AuthorRepository.NAME_PROPERTY,
				searchValue, MatchMode.ANYWHERE));
		return this.getHibernateTemplate().findByCriteria(criteria).size();
	}

	@SuppressWarnings(AuthorRepository.UNCHECKED)
	public List<? extends Author> getResults(final String searchValue,
			final int first, final int count) {
		final DetachedCriteria criteria = this.createDetachableCriteria();
		criteria.add(Restrictions.ilike(AuthorRepository.NAME_PROPERTY,
				searchValue, MatchMode.ANYWHERE));
		return this.getHibernateTemplate().findByCriteria(criteria, first,
				count);
	}

}
